#region using

using System;
using Microsoft.SharePoint.WebPartPages;

#endregion

namespace PS.WebParts.Rollup.GridViewWebPart
{
    internal interface IDataWebPart
    {
        string DataSourcesString { get; set; }
    }

    internal class DataSourceToolPart<A>
        : ToolPart
    where A : IDataWebPart
    {
        protected ToolPartBuilder builder;
        protected A owner;

        private string dataSourceID;

        public DataSourceToolPart(A owner, string title)
        {
            this.owner = owner;
            Title = title;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            builder.RegisterField(dataSourceID, owner.DataSourcesString);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            builder = new ToolPartBuilder(this);
            dataSourceID = builder.CreateToolText("DataSources", "To edit DataSources, click on the button below", "Edit DataSources...");
        }

        public override void ApplyChanges()
        {
            EnsureChildControls();
            owner.DataSourcesString = builder.RetrieveValue(dataSourceID);
        }
    }
}
